package com.hitsoysauce.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class ShowRequestHeaderServlet extends HttpServlet {

	private static final long serialVersionUID = -2922143739743886129L;
	
	
	private String keyword;
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		resp.setContentType("text/html");
		
		PrintWriter out = resp.getWriter();
		String title = "Servlet Example: show RequestHeader";
		
		String docType = "<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'>";
		
		out.println(docType +
				"<html>\n" +
				"<head><title>" + title + "</title></head>\n" +
				"<body bgcolor=\"#FDF5E6\">\n" +
				"<h1 align=\"center\">" + title + "</h1>\n" +
				"<b>Request Method:</b>" +
				req.getMethod() + "<br>\n" +
				"<b>Request URI:</b>" +
				req.getRequestURI() + "<br>\n" +
				"<b>Request getRealPath:</b>" +
				req.getSession().getServletContext().getRealPath("/") + "<br>\n" +
				"<b>Request ContenxtPath:</b>" +
				req.getContextPath() + "<br>\n" +
				"<b>Request ServletPath:</b>" +
				req.getServletPath() + "<br>\n" +
				"<b>Request Scheme:</b>" +
				req.getScheme() + "<br>\n" +
				"<b>Request ServerName:</b>" +
				req.getServerName() + "<br>\n" +
				"<b>Request Port:</b>" +
				req.getServerPort() + "<br>\n" +
				"<b>Request QueryString:</b>" +
				req.getQueryString() + "<br>\n" +
				"<b>Request getLocalAddr:</b>" +
				req.getLocalAddr() + "<br>\n" +
				"<b>Request getRemoteAddr:</b>" +
				req.getRemoteAddr() + "<br>\n" +
				"<b>Request Protocal:</b>" +
				req.getProtocol() + "<br>\n" +
				"<b>user-agent:</b>" +
				req.getHeader("user-agent") + "<br>\n" +
				"<b>init Parameter:</b>" +
				keyword + "<br>\n" +
				"<b>ServletContext init Parameter:</b>" +
				getServletContext().getInitParameter("test") + "<br>\n" +
				"<table style=\"border:1px solid #000;\" align=\"center\">\n" +
				"<tr bgcolor='#FFAD00'>\n" +
				"<th>header Name<TH>header value\n");
				
		Enumeration headerNames = req.getHeaderNames();
		
		
		while(headerNames.hasMoreElements()) {
			String headerName = (String)headerNames.nextElement();
			out.println("<tr style=\"border:1px solid #000;\"><td>" + headerName);
			out.println(" <td>" + req.getHeader(headerName));
		}
		
		out.println("</table>\n");
		out.println("</body>\n");
		out.println("</html>\n");
		
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doGet(req, resp);
	}

	@Override
	public void destroy() {
		super.destroy();
	}

	@Override
	public void init() throws ServletException {
		keyword = getServletConfig().getInitParameter("keyword");
	}

}
